package com.wytec.xywzxw.controller;

import com.wytec.xywzxw.bean.commonBean.CommonResult;
import com.wytec.xywzxw.service.VerifyCodeService;
import com.wytec.xywzxw.utils.CommonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "验证码模块")
public class VerifyCodeController extends BaseApiController{
    @Resource
    VerifyCodeService verifyCodeService;

    @GetMapping("/verify/sendEmailCode")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "模板编号 默认1"),
            @ApiImplicitParam(name = "email", value = "邮箱地址")
    })
    @ApiOperation(value = "发送邮箱验证码")
    private CommonResult<Object> sendEmailCode(@RequestParam(value = "type",defaultValue = "1") Integer type,
                                       @RequestParam(value = "email",defaultValue = "372855309@qq.com") String email){
        String code = CommonUtils.createCaptchaCode(6);
        Boolean res = verifyCodeService.sendEmailVerifyCode(code, email,type);
        if (res){
            return CommonResult.successNoMessage();
        }
        return CommonResult.failed("验证码发送失败");
    }
    @GetMapping("/verify/sendMobileCode")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "模板编号 默认1"),
            @ApiImplicitParam(name = "mobile", value = "手机号")
    })
    @ApiOperation(value = "发送手机验证码")
    private CommonResult<Object> sendMobileCode(@RequestParam(value = "type",defaultValue = "1") Integer type,
                                       @RequestParam(value = "mobile",defaultValue = "1") String mobile){
        String code = CommonUtils.createCaptchaCode(6);
        Boolean res = verifyCodeService.sendMobileVerifyCode(code, mobile,type);
        if (res){
            return CommonResult.successNoMessage();
        }
        return CommonResult.failed("验证码发送失败");
    }
    @GetMapping("/verify/checkCode")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "验证码"),
            @ApiImplicitParam(name = "account", value = "手机号/邮箱")
    })
    @ApiOperation(value = "验证验证码")
    private CommonResult<Object> checkCode(@RequestParam(value = "code") String code,
                                        @RequestParam(value = "account") String account) {
        Boolean res = verifyCodeService.checkCode(code, account);
        if (res){
            return CommonResult.successNoMessage();
        }
        return CommonResult.failed("验证码错误或已失效");
    }

}
